import VueRouter from "vue-router";

import Home from "../pages/Home";
import Detail from "../pages/Detail";
import Goods from "../pages/Goods";

const router = new VueRouter({
  mode: "history",
  routes: [
    {
      name: "jia",
      path: "/home",
      component: Home,
      meta: {
        title: "首页",
      },
      children: [
        {
          name: "shangpin",
          path: "goods/:fruit",
          component: Goods,
          meta: {
            title: "商品",
            isAuth: true,
          },
          // props: true,
          props($route) {
            return {
              fruit: $route.params.fruit,
            };
          },
          // 独享路由守卫
          beforeEnter: (_, from, next) => {
            if(localStorage.getItem("token") === "password"){
              next();
            }else{
              alert("没有权限");
            }
          }
        },
      ],
    },
    {
      name: "xiangqing",
      path: "/detail",
      component: Detail,
      meta: {
        title: "详情页",
      },
    },
  ],
});

// 全局路由守卫
/* router.beforeEach((to, from, next) => {
  if (to.meta.isAuth && localStorage.getItem("token") != "password") {
    alert("没有权限");
    return;
  }
  next();
}); */
router.afterEach((to, from) => {
  document.title = to.meta.title || "Vue";
})

export default router;
